package com.example.demo.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.example.demo.bean.Photo;
import com.example.demo.service.OssService;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OssServiceImpl implements OssService {
    private static OSSClient ossClient = null;
    private static String  bucketName = "wx-love";


    static {
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAIrRo0uowZOgQm";
        String accessKeySecret = "17ZmCRGxBNS9kL1FhriN8piBdWBmJo";

        ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }

    @Override
    public URL getUrl(String key) {
        Date date = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 3600L * 1000);
        return ossClient.generatePresignedUrl(bucketName, key, date);
    }
    @Override
    public void upload(String key, InputStream inputStream) throws OSSException {
        ossClient.putObject(bucketName, key, inputStream);
    }


    @Override
    public void upload(String key,File file) throws OSSException {
        ossClient.putObject(bucketName, key, file);
    }
}
